<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */

namespace App\Http\Admin\Process\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
//use App\Eloquent\Ygt\Permission;
use App\Http\Admin\Company\Models\Permission;
use App\Eloquent\Ygt\PrivilegePermission;
use App\Eloquent\Ygt\Process;

class PermissionController extends Controller
{
    public function index()
    {
    }
    public function lists()
    {
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];

        $process_id         = request('id');
        $where              = ['company_id'=>$company_id, 'type'=>1, 'type_id'=>$process_id];
        $lists              = Permission::getList( $where );
        $action_json        = Permission::getTypeActionJson();
//        var_dump($action_json);exit;
        return $this->view('lists', compact('process_id','lists', 'action_json') );
    }

    public function edit()
    {
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];
        $process_id         = request( 'process_id' );

        $params             = $this->requestJson();
        $id                 = isset($params['id']) ? $params['id'] : 0;
        $title              = $params['title'];
        $type_action        = $params['type_action'];
        if( $process_id ){
            if( $id ){
                $where          = ['id'=>$id];
                $data           = [ 'type'=>1, 'type_id'=>$process_id, 'type_action'=>$type_action, 'title'=>$title ];
                $result         = Permission::updateOneData($where, $data );
            } else {
                $where          = ['type'=>1,'type_id'=>$process_id,'type_action'=>$type_action];
                $is_exist       = Permission::getInfo( $where );
                if( $is_exist ){
                    return $this->ajaxFail( '动作类型已存在' );
                }
                $data           = [
                    'company_id'=>$company_id,'type'=>1, 'type_id'=>$process_id, 'type_action'=>$type_action, 'title'=>$title
                ];
                $result         = Permission::insertOneData( $data );
            }
            if( $result ){
                return $this->ajaxSuccess( '成功' );
            } else {
                return $this->ajaxFail( '失败' );
            }
        } else {
            return $this->ajaxFail( '失败process_id' );
        }
    }
}